﻿using Acme.ConcreteMixDesign.TopicInformations;
using Acme.ConcreteMixDesign.Topics;
using Acme.ConcreteMixDesign.Type;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Xceed.Words.NET;

namespace Acme.ConcreteMixDesign
{
    public class TopicInformationService : CrudAppService<TopicInformation, TopicInformationDto, Guid, PagedAndSortedResultRequestDto, CreateInformationDto>, ITopicInformationService
    {
        private readonly ITopicInformationRepository _topicInformationRepository;

        public TopicInformationService(ITopicInformationRepository topicInformationRepository) : base(topicInformationRepository)
        {
            _topicInformationRepository = topicInformationRepository;
        }

        public async Task<TopicInformationDto> SaveFlie(IFormFile file, InformationType type)
        {
            string appDataPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data");
            string path = Path.Combine(appDataPath, file.FileName);
            TopicInformation i = new TopicInformation();
            // 检查文件是否已存在
            if (File.Exists(path))
            {
                // 如果文件已存在，直接返回对应的 TopicInformation
                TopicInformation existingInformation = await _topicInformationRepository.FirstOrDefaultAsync(x => x.InformationName == file.FileName && x.InformationType == type);

                if (existingInformation != null)
                {
                    // 返回对应的 TopicInformation
                    return MapToGetOutputDto(existingInformation);
                }
            }
            else
            {
                using (var fileStream = new FileStream(path, FileMode.Create))
                {
                    await file.CopyToAsync(fileStream);
                }
                i = new TopicInformation
                {
                    InformationName = file.FileName,
                    InformationPath = path,
                    InformationType = type
                };
            }
            //直接转会DTO
            return MapToGetOutputDto(await _topicInformationRepository.InsertAsync(i));

        }

        public async Task<List<TopicInformationDto>> GetInfor()
        {
            var Info = await _topicInformationRepository.GetListAsync();
            var InfoDtos = new List<TopicInformationDto>();
            if (Info != null)
            {
                InfoDtos = ObjectMapper.Map<List<TopicInformation>, List<TopicInformationDto>>(Info);
            }
            return InfoDtos;
        }
        public async Task<List<TopicInformationDto>> GetInforMapByType(InformationType informationType)
        {
            var Info = await _topicInformationRepository.GetListAsync(x => x.InformationType == informationType);
            var InfoDtos = new List<TopicInformationDto>();
            if (Info != null)
            {
                InfoDtos = ObjectMapper.Map<List<TopicInformation>, List<TopicInformationDto>>(Info);
            }
            return InfoDtos;
        }

        public async Task<FileDto> GetMaterialAsync(Guid Inforid)
        {
            if(Inforid != Guid.Empty)
            {
                var MaterialEnity = await _topicInformationRepository.GetAsync(Inforid);
                if (File.Exists(MaterialEnity.InformationPath))
                {
                    using (FileStream fileStream = new FileStream(MaterialEnity.InformationPath, FileMode.Open))
                    {
                        MemoryStream memoryStream = new MemoryStream();
                        fileStream.CopyTo(memoryStream);
                        memoryStream.Seek(0, SeekOrigin.Begin);
                        return new FileDto
                        {
                            FileName = MaterialEnity.InformationName,
                            FileStream = memoryStream
                        };
                    }
                }
            }
                    return null;
        }
    }
}
